package org.gruposp2p.dnie.ui.dialogs;

import dniegp2pclient.DNIeGP2PClientApp;
import java.util.Enumeration;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author jj
 */
public class SelectCertDialog extends javax.swing.JDialog {

    private static Logger logger = LoggerFactory.getLogger(SelectCertDialog.class);

    private DefaultListModel listModel;
    public State state;
    public enum State {ACCEPT, CANCEL};
    private ResourceMap resourceMap;

    /** Creates new form SelectCertDialog */
    public SelectCertDialog(java.awt.Frame parent, boolean modal,
            Enumeration<String> certAliases) {
        super(parent, modal);
        initComponents();
        listModel = new DefaultListModel();
        while (certAliases.hasMoreElements()) {
            String alias = certAliases.nextElement();
            listModel.addElement(alias);
        }
        certList.setModel(listModel);
        certList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        certList.setVisibleRowCount(3);
        certList.setSelectedIndex(0);
        this.state = State.CANCEL;
        this.setLocationRelativeTo(parent);
        resourceMap = Application.getInstance(DNIeGP2PClientApp.class)
                .getContext().getResourceMap(SelectCertDialog.class);
        setTitle(resourceMap.getString("dialogTitle.text"));
    }

    public String getSelectedCertAlias () {
        int index = certList.getSelectedIndex();
        return (String) listModel.getElementAt(index);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cancelButton = new javax.swing.JButton();
        acceptButton = new javax.swing.JButton();
        certListScrollPane = new javax.swing.JScrollPane();
        certList = new javax.swing.JList();
        textLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/button_accept_16x16.png"))); // NOI18N
        cancelButton.setText("Aceptar");
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        acceptButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/cancel_16x16.png"))); // NOI18N
        acceptButton.setText("Cerrar");
        acceptButton.setName("acceptButton"); // NOI18N
        acceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptButtonActionPerformed(evt);
            }
        });

        certListScrollPane.setName("certListScrollPane"); // NOI18N

        certList.setBorder(null);
        certList.setName("certList"); // NOI18N
        certListScrollPane.setViewportView(certList);

        textLabel.setText("Seleccione uno de los certicados de la lista:");
        textLabel.setName("textLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(certListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(textLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cancelButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addComponent(acceptButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(textLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(certListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(acceptButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.state = State.ACCEPT;
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptButtonActionPerformed
        this.state = State.CANCEL;
        this.dispose();
    }//GEN-LAST:event_acceptButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acceptButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JList certList;
    private javax.swing.JScrollPane certListScrollPane;
    private javax.swing.JLabel textLabel;
    // End of variables declaration//GEN-END:variables

}
